package com.zonski.jbots.game.entity;

import com.zonski.jbots.engine.entity.AbstractMind;
import com.zonski.jbots.engine.entity.MovingEntity;
import com.zonski.jbots.engine.entity.Mind;
import com.zonski.jbots.engine.*;
import com.zonski.jbots.game.EntityHelper;
import com.zonski.jbots.game.EntityFilter;
import com.zonski.jbots.game.JBotsConstants;

import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: Chris Glover
 * Date: Oct 14, 2003
 * Time: 11:56:24 AM
 * To change this template use Options | File Templates.
 */
public class TurretMind extends AbstractMind implements EntityFilter
{
    private static final int CHECK_INTERVAL = 10;
    private int targetDirection;
    private boolean seeking;

    public TurretMind()
    {
    }

    public void update(Engine engine)
    {
        if(this.getEntity().getMode() == Modes.DEAD)
        {
            this.getEntity().setRemovable(true);
            try
            {
                Entity charge = EntityHelper.addEntity(JBotsConstants.CHARGE, this.getEntity(), engine, 0);
                EntityHelper.centreAtClosestTile(
                        charge.bounds, this.getEntity().bounds, engine.getRoom()
                );
            }catch(Exception ex){
                ex.printStackTrace();
            }
            return;
        }
        if(seeking)
        {
            int direction = this.getEntity().direction;
            if(direction == this.targetDirection && Directions.isCardinal(this.targetDirection))
            {
                UsableResource gun = (UsableResource)this.getEntity().getResource(JBotsConstants.GUN);
                gun.startUsing((this.targetDirection));
                seeking = false;
            }else if(this.getEntity().getEffectiveModeUpdates() % (CHECK_INTERVAL/6) == 0){
                // TODO : follow shortest path
                int add;
                if(this.targetDirection  - direction > Directions.MAX_DIRECTION/2 ||
                        this.targetDirection - direction < 0)
                {
                    add = -1;
                }else{
                    add = 1;
                }
                ((MovingEntity)this.getEntity()).setTargetDirection(
                        Directions.addDirection(direction, add)
                );
            }

        }
        if(engine.getUpdates() % CHECK_INTERVAL == 0)
        {
            // look for something to shoot in one of the directions
            int direction;
            UsableResource gun = (UsableResource)this.getEntity().getResource(JBotsConstants.GUN);
            if(gun.isBeingUsed())
            {
                direction = this.getEntity().direction;
            }else{
                direction = (engine.getUpdates() / CHECK_INTERVAL) % 4;
                switch(direction)
                {
                    case 0:
                        direction = Directions.NORTH;
                        break;
                    case 1:
                        direction = Directions.SOUTH;
                        break;
                   case 2:
                        direction = Directions.EAST;
                        break;
                   case 3:
                        direction = Directions.WEST;
                        break;
                }
            }
            int tileSize = engine.getRoom().getTileSize();
            int tx = EntityHelper.getCentreTileX(this.getEntity().bounds, tileSize);
            int ty = EntityHelper.getCentreTileY(this.getEntity().bounds, tileSize);
            QuickVector interesting = EntityHelper.getInterestingEntities(this, tx, ty, direction, engine);
            seeking = interesting != null && interesting.size() > 0;
            if(!seeking)
            {
                gun.stopUsing();
            }
            this.targetDirection = direction;
        }

    }

    // from entity filter
    public boolean isInteresting(Entity entity)
    {
        return entity.layerId != this.getEntity().layerId &&
                JBotsConstants.isBot(entity.entityType.getId());
    }

    public boolean isStopper(Entity entity)
    {
        return entity != this.getEntity() &&
                (JBotsConstants.isObstacle(entity.entityType.getId()) ||
                JBotsConstants.isBot(entity.entityType.getId()));
    }

    public Mind copy()
    {
        return new TurretMind();
    }
}
